
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0829
 * @Author : WangRuoyu 
 * @Date : 2023/9/17 17:23
 */

#include "iostream"

using namespace std;

const int N = 1e5 + 10;
int st[N];
int l = 0;
int r = 0;

bool empty() {
    return l == r;
}

void push(int x) {
    st[r++] = x;
}

void pop() {
    if (!empty()) {
        l++;
    }
}

int query() {
    if (!empty()) {
        return st[l];
    }
    return NULL;
}


int main() {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        string op;
        cin >> op;
        if (op == "push") {
            int x;
            scanf("%d", &x);
            push(x);
        } else if (op == "pop") {
            pop();
        } else if (op == "empty") {
            if (empty()) {
                printf("YES\n");
            } else {
                printf("NO\n");
            }
        } else if (op == "query") {
            printf("%d\n", query());
        }
    }

    return 0;
}